
Page({

  data: {
    flyText: "请输入您要的文字",
    flyDuration: 8000,
    textColor: "rgb(255,255,255)",//255,0,155
    textShadow:"3px 0 20px rgb(255,0,155), -3px 0 20px rgb(255,0,155), 0 3px 20px rgb(255,0,155), 0 -3px 20px rgb(255,0,155), 3px 3px 20px rgb(255,0,155), -3px -3px 20px rgb(255,0,155), 3px -3px 20px rgb(255,0,155), -3px 3px 20px rgb(255,0,155);",
    isShowMenu:false,
    isShowMenu_style:"hidden",
    isShowChangeText: false,
    isShowChangeText_style: "hidden",
    isShowSetting_style:"hidden",
    isInputContentFocus:false,
    isShowText:true,
    rotate_value: "rotate(90deg)",
    textFontSize:"140px",
    backgroundColor:"#000",
    isShowHelper_style:"hidden",
    //定时器
    inter_id:null,
    textMarginBottom:'-60rpx',
    nowtime_id:null,
    num: 1,
    // 使用data数据对象设置样式名  
    minusStatus: 'disabled',
    isShowDao_style: "hidden",
    dao_inter:null
  },
  onShareAppMessage: function (res) {

    return {

      title: '发现了一个超级好玩的小程序，手持弹幕',

      desc: this.data.flyText,

      path: '/page/dm?str=' + this.data.flyText,
      success: function (res) {
      }

    }

  },

  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    if (options.str){
      this.setData({
        flyText: options.str
      });
    }

    wx.getSystemInfo({
      success: res => {
        this.setData({
          screenHeight: res.screenHeight
        });
      }
    });
    //分享
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  onReady: function () {
    this.initBarrage(this.data.flyText);
  },
  show_menu:function(){
    if (this.data.isShowMenu){
      this.setData({
        isShowMenu_style : "hidden",
        isShowMenu : false
      })
    }else{
      if (this.data.isShowChangeText_style != "visible" || this.data.isShowSetting_style != "visible" || this.data.isShowHelper_style != "visible" || this.data.isShowDao_style != "visible"){
        this.setData({
          isShowMenu_style: "visible",
          isShowMenu: true
        })
      }
    }
    //初始化
    this.setData({
      isShowChangeText_style: "hidden",
      isShowText: true,
      isShowSetting_style: "hidden",
      isShowHelper_style: "hidden",
      isShowDao_style:"hidden"
    })

  },
  //点击更改文字
  change_text:function(){
      this.setData({
        isShowChangeText_style: "visible",
        isShowMenu_style: "hidden",
        isShowMenu: false,
        isShowText:false
      })
  },
  to_dp:function(){
    wx.navigateTo({
      url: '../dp/dp',
    })
  },
  //随机颜色
  random_color:function(){
    //生成随机字体颜色、
    var red = Math.round(Math.random() * 255);
    var green = Math.round(Math.random() * 255);
    var blue = Math.round(Math.random() * 255);
    
    this.setData({
      textColor: "rgb(" + red+","+green+","+blue+")"
    });
    //生成文字阴影
    red = Math.round(Math.random() * 255);
    green = Math.round(Math.random() * 255);
    blue = Math.round(Math.random() * 255);
    var color = "rgb(" + red + "," + green + "," + blue + ")";
    this.setData({
      //"3px 0 20px rgb(255,0,155), -3px 0 20px rgb(255,0,155), 0 3px 20px rgb(255,0,155), 0 -3px 20px rgb(255,0,155), 3px 3px 20px rgb(255,0,155), -3px -3px 20px rgb(255,0,155), 3px -3px 20px rgb(255,0,155), -3px 3px 20px rgb(255,0,155);",
      textShadow: "3px 0 20px " + color + ", -3px 0 20px " + color + ", 0 3px 20px " + color + ", 0 -3px 20px " + color + ", 3px 3px 20px " + color + ", -3px -3px 20px " + color + ", 3px -3px 20px " + color +", -3px 3px 20px "+color
    });

  },
  open_setting:function(){
      this.setData({
        isShowSetting_style: "visible",
        isShowMenu_style: "hidden",
        isShowMenu: false
      })

  },
  open_helper:function(){
    this.setData({
      isShowHelper_style: "visible",
      isShowMenu_style: "hidden",
      isShowMenu: false
    })

  },
  open_more:function(){
   


    //wx.showToast({ title: '更多敬请期待...', icon: "success", duration: 2000 })
  },
  change_direction:function(e){
    if (e.target.dataset.type == "row"){
      this.setData({
        rotate_value: "rotate(90deg)"
      })
    }
    if (e.target.dataset.type == "col"){
      this.setData({
        rotate_value: "rotate(0deg)"
      })
      
    }
  },
  //改变移动速度
  change_speed:function(e){
    if(e.target.dataset.type=="man"){
      this.setData({
        flyDuration:12000
      })
    } else if (e.target.dataset.type == "nor"){
      this.setData({
        flyDuration: 8000
      })
    }else{
      this.setData({
        flyDuration: 4000
      })
    }
  },
  //改变字体大小
  change_fontSize:function(e){
    if (e.target.dataset.type == "small") {
      this.setData({
        textFontSize: "120px"
      })
    } else if (e.target.dataset.type == "zhong") {
      this.setData({
        textFontSize: "140px"
      })
    } else if (e.target.dataset.type == "big") {
      this.setData({
        textFontSize: "160px"
      })
    } else if (e.target.dataset.type == "superbig") {
      this.setData({
        textFontSize: "180px"
      })
    } 

  },
  //设置文字发光
  change_fontShadow:function(e){
    if (e.target.dataset.type == "close") {
      this.setData({
        textShadow:""
      })
    }else{
      var color = e.target.dataset.type;
      this.setData({
        textShadow: "3px 0 20px " + color + ", -3px 0 20px " + color + ", 0 3px 20px " + color + ", 0 -3px 20px " + color + ", 3px 3px 20px " + color + ", -3px -3px 20px " + color + ", 3px -3px 20px " + color + ", -3px 3px 20px " + color
      });
    }
  },
  change_fontColor:function(e){
    this.setData({
      textColor: e.target.dataset.type
    });
  },
  change_backColor:function(e){
    this.setData({
      backgroundColor: e.target.dataset.type
    });
  },
  //当前时间
  now_time:function(){
    // //动画恢复到原位
    // this.animation.translateY(0).step();
    // this.setData({
    //   //textArr: [], //文字清空的动画效果
    //   animationData: this.animation.export()
    // });
    // //清除定时器
    // clearInterval(this.data.inter_id);
    if(this.data.nowtime_id == null){
      var nowtime_id = setInterval(function () {
        //获取当前时间
        var myDate = new Date();
        myDate.getHours();       //获取当前小时数(0-23)
        myDate.getMinutes();     //获取当前分钟数(0-59)
        myDate.getSeconds();     //获取当前秒数(0-59)
        myDate.getMilliseconds();    //获取当前毫秒数(0-999)
        var str = myDate.getHours() + ":" + (myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes()) + ":" + (myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds()) + "." + parseInt(myDate.getMilliseconds() / 100);
        this.setData({
          textArr: str.split(""),
          flyText: str,
          textMarginBottom: '-160rpx'
        });

      }.bind(this), 1000 / 60);
      //设置定时器
      this.setData({
        nowtime_id: nowtime_id
      });

    }else{
      clearInterval(this.data.nowtime_id);
      var str = "请输入您要的文字";
      this.setData({
        nowtime_id: null,
        flyText: str,
        textMarginBottom:'-60rpx'
      });
    }

    

  },
  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回  
    this.setData({
      num: num
    });
  },
  open_dao:function(){
    if (this.data.dao_inter){
      clearInterval(this.data.dao_inter);
      var str = "请输入您要的文字";
      this.setData({
        dao_inter:null,
        flyText: str,
        textMarginBottom: '-60rpx'
      });
    }else{
      this.setData({
        isShowDao_style: "visible",
        isShowMenu_style: "hidden",
        isShowMenu: false
      })
    }
  },
  start_dao:function(){
    if (this.data.dao_inter==null){
      var se = this.data.num * 60;
      var dao_inter = setInterval(function () {
        if (se <= 0) {
          clearInterval(this.data.dao_inter);
          this.setData({
            dao_inter: null,
            textMarginBottom: '-60rpx'
          });
        }
        var str = parseInt(se / (60 * 60)) + ":" + parseInt(se / (60)) + ":" + parseInt(se);
        this.setData({
          dao_inter: dao_inter,
          textArr: str.split(""),
          flyText: str,
          textMarginBottom: '-160rpx'
        });
        se--;
      }.bind(this), 1000)
    }else{
      clearInterval(this.data.dao_inter);
      this.setData({
        dao_inter: null,
        textMarginBottom: '-60rpx'
      });
    }
    
    
  },
  //初始化弹幕
  initBarrage: function (flyText) {
    var textArr = flyText.split("");
    var screenHeight = this.data.screenHeight*1.3; //rpx计算调整位置
    var transYHeight = screenHeight * textArr.length / 5; //px 计算动画移动Y值
    // console.log(screenHeight);

    this.setData({
      screenHeight: screenHeight,
      transYHeight: transYHeight,
      textArr: textArr,
    });

    //开始循环执行
    this.barrageAnimation();

    //定时执行
    var inter_id = setInterval(function () {
      this.barrageAnimation();
    }.bind(this), this.data.flyDuration);
    //设置定时器
    this.setData({
      inter_id:inter_id
    })
  },

  //定时器 让弹幕飞起来 
  barrageAnimation: function () {
    //开始弹幕滚动
    if (!this.animation) {
      this.animation = wx.createAnimation({
        duration: 0,
        timingFunction: 'linear'
      });
    }

    //动画恢复到原位
    this.animation.translateY(this.data.screenHeight).step();
    this.setData({
      textArr: [], //文字清空的动画效果
      animationData: this.animation.export()
    });

    //延迟0.1s执行
    setTimeout(function () {
      this.animation.translateY(-this.data.transYHeight).step({ duration: this.data.flyDuration });
      this.setData({
        textArr: this.data.flyText.split(""),
        animationData: this.animation.export()
      });
    }.bind(this), 100);

  },

  //设置弹幕
  formSubmit: function (e) {
    var flyText = e.detail.value["flyText"];
    if (flyText) {
      var textArr = flyText.split("");
      var screenHeight = this.data.screenHeight*1.3; //rpx计算调整位置
      var transYHeight = screenHeight * textArr.length / 5; //px 计算动画移动Y值

      this.setData({
        flyText: flyText,
        screenHeight: screenHeight,
        transYHeight: transYHeight,
        textArr: textArr,
      });

    }
  }

})

